<?php

namespace IYUU\sdk\CoolQ\MiddleWare;

use IYUU\sdk\CoolQ\Context;
use IYUU\sdk\CoolQ\MiddlewareInterface;
use Throwable;
use function IYUU\sdk\CoolQ\getOneBot;

/**
 * Redis延迟队列回调
 * Class EventListen
 * @package IYUU\sdk\CoolQ\MiddleWare
 */
class RedisQueueCallback implements MiddlewareInterface
{
    public function process(Context $context, callable $next): array
    {
        try {
            $topic = $context->get('topic');
            $callback = $context->get('callback');
            $job_sign = $context->get('job_sign');
            if ($topic && $callback && $job_sign) {
                $action = $context->get('body.action', '');
                switch ($action) {
                    case 'delete_msg':      // 撤回消息
                        $message_id = $context->get('body.message_id', 0);
                        getOneBot()->deleteMsg($message_id);
                        break;
                    default:
                        break;
                }
                echo 'ok';
                exit(0);
            }
        } catch (Throwable $exception) {
        }

        return $next($context);
    }
}
